import request from '@/config/axios'
import type { RegisterVO, UserLoginVO } from './types'
import { defHttp } from '@/utils/http/axios';
import { RequestEnum } from '@/enums/httpEnum';
import { ServicePrefixEnum } from '@/enums/commonEnum';
import type { AxiosRequestConfig } from 'axios';

const AUTH_MODULAR = '/system/auth';
const TENANT_MODULAR = '/system/tenant';

export const Api = {
  Login: {
    url: `${ServicePrefixEnum.BASE}/${AUTH_MODULAR}/login`,
    method: RequestEnum.POST,
  } as AxiosRequestConfig,
  Register: {
    url: `${ServicePrefixEnum.BASE}/${AUTH_MODULAR}/register`,
    method: RequestEnum.POST,
  } as AxiosRequestConfig,
  GetTenantIdByName: {
    url: `${ServicePrefixEnum.BASE}/${TENANT_MODULAR}/get-by-website`,
    method: RequestEnum.GET,
  } as AxiosRequestConfig,
  LoginOut: {
    url: `${ServicePrefixEnum.BASE}/${AUTH_MODULAR}/logout`,
    method: RequestEnum.POST,
  } as AxiosRequestConfig,
  GetInfo: {
    url: `${ServicePrefixEnum.BASE}/${AUTH_MODULAR}/get-permission-info`,
    method: RequestEnum.GET,
  } as AxiosRequestConfig,
  SendSmsCode: {
    url: `${ServicePrefixEnum.BASE}/${AUTH_MODULAR}/send-sms-code`,
    method: RequestEnum.POST,
  } as AxiosRequestConfig,
  SmsLogin: {
    url: `${ServicePrefixEnum.BASE}/${AUTH_MODULAR}/sms-login`,
    method: RequestEnum.POST,
  } as AxiosRequestConfig,
  SocialLogin: {
    url: `${ServicePrefixEnum.BASE}/${AUTH_MODULAR}/social-login`,
    method: RequestEnum.POST,
  } as AxiosRequestConfig,
  SocialAuthRedirect: {
    url: `${ServicePrefixEnum.BASE}/${AUTH_MODULAR}/social-auth-redirect`,
    method: RequestEnum.GET,
  } as AxiosRequestConfig,
  SmsResetPassword: {
    url: `${ServicePrefixEnum.BASE}/${AUTH_MODULAR}/reset-password`,
    method: RequestEnum.POST,
  } as AxiosRequestConfig,
}


export const login = (data: UserLoginVO) => {
  return defHttp.request({ ...Api.Login,params:data})
}

export const register = (data: RegisterVO) => {
  return defHttp.request({ ...Api.Register,params:data})
}

// 使用租户名，获得租户编号
export const getTenantIdByName = (name: string) => {
  let params = {name:name}
  return defHttp.request({ ...Api.GetTenantIdByName,params:params})
}

// 登出
export const loginOut = () => {
  return defHttp.request({ ...Api.LoginOut})
}

// 获取用户权限信息
export const getInfo = () => {
  return defHttp.request({ ...Api.GetInfo})
}

//获取登录验证码
export const sendSmsCode = (data: SmsCodeVO) => {
  return defHttp.request({ ...Api.SendSmsCode,params:data})
}

// 短信验证码登录
export const smsLogin = (data: SmsLoginVO) => {
  return defHttp.request({ ...Api.SmsLogin,params:data})
}

// 社交快捷登录，使用 code 授权码
export function socialLogin(type: string, code: string, state: string) {
  let params = {data:{type, code, state}}
  return defHttp.request({ ...Api.SocialLogin,params:params})
}

// 社交授权的跳转
export const socialAuthRedirect = (type: number, redirectUri: string) => {
  let params = {type, redirectUri}
  return defHttp.request({ ...Api.SocialAuthRedirect,params:params})
}

// 通过短信重置密码
export const smsResetPassword = (data: any) => {
  return defHttp.request({ ...Api.SmsResetPassword,params:data})
}


export interface SmsCodeVO {
  mobile: string
  scene: number
}

export interface SmsLoginVO {
  mobile: string
  code: string
}

// 登录
// export const login = (data: UserLoginVO) => {
//   return request.post({ url: '/system/auth/login', data })
// }
//
// // 注册
// export const register = (data: RegisterVO) => {
//   return request.post({ url: '/system/auth/register', data })
// }

// 使用租户名，获得租户编号
// export const getTenantIdByName = (name: string) => {
//   return request.get({ url: '/system/tenant/get-id-by-name?name=' + name })
// }

// 使用租户域名，获得租户信息
// export const getTenantByWebsite = (website: string) => {
//   return request.get({ url: '/system/tenant/get-by-website?website=' + website })
// }
//
// // 登出
// export const loginOut = () => {
//   return request.post({ url: '/system/auth/logout' })
// }
//
// // 获取用户权限信息
// export const getInfo = () => {
//   return request.get({ url: '/system/auth/get-permission-info' })
// }
//
// //获取登录验证码
// export const sendSmsCode = (data: SmsCodeVO) => {
//   return request.post({ url: '/system/auth/send-sms-code', data })
// }
//
// // 短信验证码登录
// export const smsLogin = (data: SmsLoginVO) => {
//   return request.post({ url: '/system/auth/sms-login', data })
// }
//
// // 社交快捷登录，使用 code 授权码
// export function socialLogin(type: string, code: string, state: string) {
//   return request.post({
//     url: '/system/auth/social-login',
//     data: {
//       type,
//       code,
//       state
//     }
//   })
// }
//
// // 社交授权的跳转
// export const socialAuthRedirect = (type: number, redirectUri: string) => {
//   return request.get({
//     url: '/system/auth/social-auth-redirect?type=' + type + '&redirectUri=' + redirectUri
//   })
// }

// 获取验证图片以及 token todo
export const getCode = (data: any) => {
  debugger
  return request.postOriginal({ url: 'system/captcha/get', data })
}

// 滑动或者点选验证 todo
export const reqCheck = (data: any) => {
  return request.postOriginal({ url: 'system/captcha/check', data })
}
//
// // 通过短信重置密码
// export const smsResetPassword = (data: any) => {
//   return request.post({ url: '/system/auth/reset-password', data })
// }
